/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: CommonDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.welfare.common;

import java.io.CharArrayReader;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.*;

import oracle.sql.CLOB;

import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashList;
import com.zll.framework.util.StringUtil;



public class CommonUtil
{
	//싱글톤 구현
	private final static CommonUtil comm = new CommonUtil();
	private CommonUtil() {}
	public static CommonUtil getInstance() {
		return comm;
	}
	
	
	
	/**** 공통 코드 ****/
	private HashMap codeMap = new HashMap();

	/**** 리로드 공통코드 ****/
	public static void reloadCode() {
		comm.codeMap = new HashMap();
	}

	

	public static HashList getCodeList(String code_gubn) throws Exception
	{
		return getCodeList(code_gubn, null);
	}

	public static HashList getCodeList(String code_gubn, String allName) throws Exception
	{
		//System.out.println(code_gubn + ":" + comm.codeMap.toString());
		if (comm.codeMap.get(code_gubn) == null)
		{
			HashList list = null;

			if ("use_yn".equals(code_gubn))
			{
				list = new HashList();

				HashMap map = new HashMap();
				map.put("code", "Y");
				map.put("code_name", "사용");
				map.put("code_desc", "");
				list.add(map);

				map = new HashMap();
				map.put("code", "N");
				map.put("code_name", "미사용");
				map.put("code_desc", "");
				list.add(map);
			}
			else if ("open_yn".equals(code_gubn))
			{
				list = new HashList();

				HashMap map = new HashMap();
				map.put("code", "Y");
				map.put("code_name", "공개");
				map.put("code_desc", "");
				list.add(map);

				map = new HashMap();
				map.put("code", "N");
				map.put("code_name", "비공개");
				map.put("code_desc", "");
				list.add(map);
			}
			else
			{
				HashMap infos = new HashMap();
				infos.put("code_gubn", code_gubn);
				infos.put("use_yn", "Y");
				list = DBManager.executeQuery("/common.xml", "select_code_list", infos);
			}

			//싱글톤 코드에 저장.
			comm.codeMap.put(code_gubn, list);

			//System.out.println("code map" + codeMap.toString());
		}


		HashList list = (HashList)comm.codeMap.get(code_gubn);


		if (allName == null) {
			return list;
		} else {
			HashList allList = new HashList();
			HashMap map = new HashMap();
			map.put("code", "");
			map.put("code_name", allName);
			map.put("code_desc", "");
			allList.add(map);
			allList.add(list);

			return allList;
		}
	}

	public static HashList getCategoryList() throws Exception
	{
		HashMap infos = new HashMap();
		return DBManager.executeQuery("/common.xml", "select_category_list", infos);
	}

	public static HashMap getCode(String dept_gbn, String code_gubn, String code) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("dept_gbn", 	dept_gbn);
		infos.put("code_gubn", 	code_gubn);
		infos.put("code",		code);
		HashList HashList = DBManager.executeQuery("/common.xml", "select_code_list", infos);
		if (HashList.size() == 1) {
			return (HashMap)HashList.get(0);
		} else {
			return new HashMap();
		}
	}


	public static String nextDay(String from_date, int cnt) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("from_date", from_date);
		infos.put("cnt", 	String.valueOf(cnt));
		HashList cList = DBManager.executeQuery("/common.xml", "select_next_day", infos);
		return cList.get("to_date", 0);
	}
	
	
	public static HashList getFileInfo(String fileId) throws Exception {
		HashMap infos = new HashMap();
		infos.put("file_id", fileId);
		return DBManager.executeQuery("/common.xml", "select_file_info", infos);
	}
	
	public static HashList getFileList(String fileRefId) throws Exception {
		HashMap infos = new HashMap();
		infos.put("file_ref_id", fileRefId);
		return DBManager.executeQuery("/common.xml", "select_file_list", infos);
	}
	
	public static String getFileId() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		;
		return df.format(cal.getTime()) + StringUtil.lpad(String.valueOf(new Random().nextInt(9999)),4,"0");
	}

	
	public static String makeCheckBox(HashList list, String name, String selectedValue) {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < list.size(); i++) {
			String title = "";
			if ("".equals(list.get("code_desc",i))) {
				title = "";
			} else {
				title = " title=\"" + list.get("code_desc",i) + "\"";
			}
			if (!"".equals(list.get("code",i)) && selectedValue.indexOf(list.get("code",i)) > -1) {
				sb.append(" <span class=\"checkbox checked\"><nobr><input type=\"checkbox\" name=\""+name+"."+name+"\" id=\""+name+i+"\" value=\""+list.get("code",i)+"\" ");
				sb.append(" checked=\"checked\" ");
				sb.append(" /><label class=\"checked\" "+title+" for=\""+name+i+"\">"+list.get("code_name",i)+"</label></nobr></span>");
			} else {
				sb.append(" <span class=\"checkbox\"><nobr><input type=\"checkbox\" name=\""+name+"."+name+"\" id=\""+name+i+"\" value=\""+list.get("code",i)+"\" ");
				sb.append(" /><label "+title+" for=\""+name+i+"\">"+list.get("code_name",i)+"</label></nobr></span>");
			}
		}

		return sb.toString();
	}

	/**
	 * Multi checkbox 값만 표시
	 * @param list
	 * @param name
	 * @param selectedValue
	 * @return
	 */
	public static String makeCheckBoxByName(HashList list, String selectedValue) {

		StringBuffer sb = new StringBuffer();
		
		for (int i = 0; i < list.size(); i++) {
			if (selectedValue.indexOf(list.get("code",i)) > -1) {
				sb.append("|").append(list.get("code_name",i));
			} else {
				///sb.append("<div class=\"checkbox\"><input type=\"checkbox\" name=\""+name+"."+name+"\" id=\""+name+i+"\" value=\""+list.get("code",i)+"\" ");
				//sb.append(" /><label for=\""+name+i+"\">"+list.get("code_name",i)+"</label></div>");
			}
		}

		if (sb.length() > 0) {
			return sb.toString().substring(1);
		} else {
			return sb.toString();
		}
	}

	/**
	 * 나이에 해당하는 생애주기를 조회한다.
	 * @param age
	 * @return
	 * @throws Exception
	 */
	public static String convertSrchAge(String age) throws Exception {
		if ("".equals(age)) {
			return "";
		}
		HashMap infos = new HashMap();
		infos.put("age", age);
		HashList list = DBManager.executeQuery("/common.xml", "select_srch_age", infos);
		if (list.size() > 0) {
			return list.get("srch_age", 0);
		} else {
			return "";
		}
	}
	
	/**
	 * 연령대 코드로 생애주기를 조회한다.
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public static String convertSrchAgeByCode(String code) throws Exception {
		if ("".equals(code)) {
			return "";
		}
		HashMap infos = new HashMap();
		infos.put("code", code);
		HashList list = DBManager.executeQuery("/common.xml", "select_srch_age_by_code", infos);
		if (list.size() > 0) {
			return list.get("srch_age", 0);
		} else {
			return "";
		}
	}
	
	public static void updateCLOB(CLOB clob, String value) throws Exception {
        Writer writer = clob.getCharacterOutputStream();
        Reader reader = new CharArrayReader(value.toCharArray());
        char[] buffer = new char[1024];
        int read = 0;

        while ((read = reader.read(buffer, 0, 1024)) != -1) {
            writer.write(buffer, 0, read);
        }
        reader.close();
        writer.close();
	}
	

	public static HashList getPopupList() throws Exception
	{
		HashMap infos = new HashMap();
		return DBManager.executeQuery("/common.xml", "select_popup_list", infos);
	}
}
